use actix_web::{actix::Message, Error};
use chrono::{NaiveDateTime, Utc};
use common::schema::member;
use network::response::{LoginMsgs, Msgs};
use network::response::MyError;


#[derive(Debug, Serialize, Deserialize, PartialEq, Identifiable, Queryable)]
#[table_name = "member"]
pub struct Member {
    pub id: i32,
    pub email: String,
    pub username: String,
    pub mobile: String,
    pub avatar: String,
    pub password: String,
    pub created: NaiveDateTime,
    pub updated: NaiveDateTime,
}

#[derive(Debug, Serialize, Deserialize, Insertable)]
#[table_name = "member"]
pub struct NewMember<'a> {
    pub email: &'a str,
    pub username: &'a str,
    pub password: &'a str,
    pub created: NaiveDateTime,
    pub updated: NaiveDateTime,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Register {
    pub username: String,
    pub email: String,
    pub password: String,
    pub confirm_password: String,
}

impl Message for Register {
    type Result = Result<Msgs, Error>;
}

#[derive(Deserialize,Serialize, Debug)]
pub struct Login {
    pub username: String,
    pub password: String,
}

impl Message for Login {
    type Result = Result<LoginMsgs, Error>;
}

impl Member {
    pub fn new() -> Member {
        Member {
            id: 0,
            email: "".to_string(),
            username: "".to_string(),
            password: "".to_string(),
            avatar: "".to_string(),
            mobile: "".to_string(),
            created: Utc::now().naive_utc(),
            updated: Utc::now().naive_utc(),
        }
    }
}